package cn.growthgate.fgo.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionUtils {

	public static boolean isEmpty(Collection<?> c) {
		return c == null || c.isEmpty();
	}

	public static boolean isNotEmpty(Collection<?> c) {
		return !isEmpty(c);
	}

	public static boolean isAnyEmpty(Collection<?>... cs) {
		if (ArrayUtils.isEmpty(cs)) {
			return false;
		}
		for (Collection<?> c : cs) {
			if (isEmpty(c)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isNoneEmpty(Collection<?>... cs) {
		return !isAnyEmpty(cs);
	}

	public static boolean isAllEmpty(Collection<?>... cs) {
		if (ArrayUtils.isEmpty(cs)) {
			return true;
		}
		for (Collection<?> c : cs) {
			if (isNotEmpty(c)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isEmpty(Map<?, ?> m) {
		return m == null || m.isEmpty();
	}

	public static boolean isNotEmpty(Map<?, ?> m) {
		return !isEmpty(m);
	}

	public static boolean isAnyEmpty(Map<?, ?>... ms) {
		if (ArrayUtils.isEmpty(ms)) {
			return false;
		}
		for (Map<?, ?> m : ms) {
			if (isEmpty(m)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isNoneEmpty(Map<?, ?>... ms) {
		return !isAnyEmpty(ms);
	}

	public static boolean isAllEmpty(Map<?, ?>... ms) {
		if (ArrayUtils.isEmpty(ms)) {
			return true;
		}
		for (Map<?, ?> m : ms) {
			if (isNotEmpty(m)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 将集合转化为数组
	 * 
	 * @param c
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <E> E[] toArray(Collection<E> c, Class<E> type) {
		E[] array = (E[]) Array.newInstance(type, c.size());
		return c.toArray(array);
	}

	/**
	 * 取两个集合的并集
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <E> Collection<E> union(Collection<E> a, Collection<E> b) {
		return org.apache.commons.collections.CollectionUtils.union(a, b);
	}

	/**
	 * 取两个集合的交集
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <E> Collection<E> intersection(Collection<E> a, Collection<E> b) {
		return org.apache.commons.collections.CollectionUtils.intersection(a, b);
	}

	/**
	 * 取两个集合交集的补集
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <E> Collection<E> disjunction(Collection<E> a, Collection<E> b) {
		return org.apache.commons.collections.CollectionUtils.disjunction(a, b);
	}

	/**
	 * 取两个集合的差集
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <E> Collection<E> subtract(Collection<E> a, Collection<E> b) {
		return org.apache.commons.collections.CollectionUtils.subtract(a, b);
	}

	public static List<Map<String, Object>> generateSeries(Object... objs) {
		if (objs == null) {
			return null;
		}
		List<Map<String, Object>> result = new ArrayList<>(objs.length);
		for (Object obj : objs) {
			Map<String, Object> map = new HashMap<>();
			if (obj instanceof List<?>) {
				List<?> list = (List<?>) obj;
				if (list != null && list.size() >= 2) {
					map.put("value", list.get(0));
					map.put("desc", list.get(1));
				}
			} else {
				map.put("value", obj);
			}
			result.add(map);
		}
		return result;
	}

}
